package com.alex.mda.jdbc;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alex.mda.jdbc.meta.Catalog;
import com.alex.mda.jdbc.meta.Column;
import com.alex.mda.jdbc.meta.Table;

public class Test {

	private static MetaService service = MetaService.getInstance();

	public static void main(String[] args) throws Exception {
		Connection conn = service.getConn(service.getConnNames().iterator()
				.next());
		Map<String, Catalog> catalogs = service.getCatalogs(conn);
		Set<String> keySet = catalogs.keySet();
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String catalogName = (String) iterator.next();
			System.out.println(catalogName);

			// Catalog catalog = catalogs.get(catalogName);
			// System.out.println("Schemas: " + service.getSchemas(conn,
			// catalogName).size());

			Map<String, Table> tables = service.getTables(conn, catalogName);
			Set<String> tableNameSet = tables.keySet();
			for (Iterator<String> iterator2 = tableNameSet.iterator(); iterator2
					.hasNext();) {
				String tableName = (String) iterator2.next();
				System.out.print(tableName);
				System.out.print(" | ");
			}

			System.out.println();
			System.out.println("=============================");
		}

		System.out.println("Database: alex, Table: employees.");
		Table table = new Table("employees", "alex");
		Map<String, Column> columns = table.getColumns(conn);
		Set<String> colNameSet = columns.keySet();
		for (Iterator<String> iterator = colNameSet.iterator(); iterator
				.hasNext();) {
			String colName = iterator.next();
			System.out.print(colName + ", ");
			System.out.print(columns.get(colName).getJavaType());
			System.out.print(" | ");
		}
		System.out.println();
		List<String> primKeyNames = table.getPrimaryKeyNames(conn);
		for (Iterator<String> iterator = primKeyNames.iterator(); iterator
				.hasNext();) {
			String primKeyName = iterator.next();
			System.out.print(primKeyName);
			System.out.print(" | ");
		}
	}
}
